package cn.virens.library.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.drawable.Drawable;
import android.support.annotation.Nullable;
import android.support.v7.widget.AppCompatRadioButton;
import android.util.AttributeSet;

import cn.virens.library.R;


/**
 * Created by loioi on 2017/7/6.
 */

public class RadioButton extends AppCompatRadioButton {
    private int mDrawableWidth;
    private int mDrawableHeight;
    private boolean mBadge;
    private int mBadgeSize;
    private int mBadgeOffset;
    private int mBadgeColor;

    public RadioButton(Context context) {
        this(context, null);
    }

    public RadioButton(Context context, AttributeSet attrs) {
        this(context, attrs, android.support.v7.appcompat.R.attr.radioButtonStyle);
    }

    public RadioButton(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        this._init(attrs, defStyleAttr);

        Drawable[] drawables = getCompoundDrawables();
        this.setCompoundDrawables(drawables[0], drawables[1], drawables[2], drawables[3]);
    }

    private void _init(AttributeSet attrs, int defStyleAttr) {
        final TypedArray a = getContext().obtainStyledAttributes(attrs, R.styleable.RadioButton, defStyleAttr, 0);

        this.mDrawableWidth = (int) a.getDimension(R.styleable.RadioButton_drawableWidth, 0F);
        this.mDrawableHeight = (int) a.getDimension(R.styleable.RadioButton_drawableHeight, 0F);
        this.mBadge = a.getBoolean(R.styleable.RadioButton_badge, false);
        this.mBadgeSize = (int) a.getDimension(R.styleable.RadioButton_badgeSize, 0F);
        this.mBadgeOffset = (int) a.getDimension(R.styleable.RadioButton_badgeOffset, 0F);
        this.mBadgeColor = a.getColor(R.styleable.RadioButton_badgeColor, Color.RED);

        a.recycle();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        if (mBadge) {
            Paint paint = new Paint();
            paint.setColor(mBadgeColor);
            paint.setAntiAlias(true);

            int width = Math.min(getWidth(), getMeasuredWidth());
            canvas.drawCircle(width - mBadgeOffset, mBadgeOffset, mBadgeSize, paint);// 小圆
        }
        super.onDraw(canvas);
    }

    public boolean getBadge() {
        return mBadge;
    }

    public void setBadge(boolean mBadge) {
        this.mBadge = mBadge;
        invalidate();
    }

    @Override
    public void setCompoundDrawables(@Nullable Drawable left, @Nullable Drawable top, @Nullable Drawable right, @Nullable Drawable bottom) {
        top = setBounds(top);
        left = setBounds(left);
        right = setBounds(right);
        bottom = setBounds(bottom);

        super.setCompoundDrawables(left, top, right, bottom);
    }

    private Drawable setBounds(Drawable drawable) {
        if (drawable == null) return drawable;

        int width = mDrawableWidth == 0 ? drawable.getMinimumWidth() : mDrawableWidth;
        int height = mDrawableHeight == 0 ? drawable.getMinimumHeight() : mDrawableHeight;

        drawable.setBounds(0, 0, width, height);

        return drawable;
    }
}
